#!/bin/bash

# Slackware build script for Open SimH

# Copyright 2023 Antonio Leal, Porto Salvo, Oeiras, Portugal
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=open-simh
SRCNAM=simh
VERSION=${VERSION:-86b19c7}
COMMIT=${COMMIT:-86b19c767b681385cf276b498073ab6702d39591}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$COMMIT
tar xvf $CWD/$SRCNAM-$COMMIT.tar.gz
cd $SRCNAM-$COMMIT

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# make BIN/vax
# make BIN/pdp11
make all

mkdir -p $PKG/opt/$PRGNAM/bin
mv BIN/* $PKG/opt/$PRGNAM/bin

### Symlinks ##
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin ; rm -rf open-simh-3b2 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/3b2 open-simh-3b2 )
( cd $PKG/usr/bin ; rm -rf open-simh-altair )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/altair open-simh-altair )
( cd $PKG/usr/bin ; rm -rf open-simh-altairz80 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/altairz80 open-simh-altairz80 )
( cd $PKG/usr/bin ; rm -rf open-simh-b5500 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/b5500 open-simh-b5500 )
( cd $PKG/usr/bin ; rm -rf open-simh-besm6 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/besm6 open-simh-besm6 )
( cd $PKG/usr/bin ; rm -rf open-simh-cdc1700 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/cdc1700 open-simh-cdc1700 )
( cd $PKG/usr/bin ; rm -rf open-simh-eclipse )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/eclipse open-simh-eclipse )
( cd $PKG/usr/bin ; rm -rf open-simh-gri )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/gri open-simh-gri )
( cd $PKG/usr/bin ; rm -rf open-simh-h316 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/h316 open-simh-h316 )
( cd $PKG/usr/bin ; rm -rf open-simh-hp2100 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/hp2100 open-simh-hp2100 )
( cd $PKG/usr/bin ; rm -rf open-simh-hp3000 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/hp3000 open-simh-hp3000 )
( cd $PKG/usr/bin ; rm -rf open-simh-i1401 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i1401 open-simh-i1401 )
( cd $PKG/usr/bin ; rm -rf open-simh-i1620 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i1620 open-simh-i1620 )
( cd $PKG/usr/bin ; rm -rf open-simh-i650 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i650 open-simh-i650 )
( cd $PKG/usr/bin ; rm -rf open-simh-i701 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i701 open-simh-i701 )
( cd $PKG/usr/bin ; rm -rf open-simh-i7010 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i7010 open-simh-i7010 )
( cd $PKG/usr/bin ; rm -rf open-simh-i704 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i704 open-simh-i704 )
( cd $PKG/usr/bin ; rm -rf open-simh-i7070 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i7070 open-simh-i7070 )
( cd $PKG/usr/bin ; rm -rf open-simh-i7080 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i7080 open-simh-i7080 )
( cd $PKG/usr/bin ; rm -rf open-simh-i7090 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i7090 open-simh-i7090 )
( cd $PKG/usr/bin ; rm -rf open-simh-i7094 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/i7094 open-simh-i7094 )
( cd $PKG/usr/bin ; rm -rf open-simh-ibm1130 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/ibm1130 open-simh-ibm1130 )
( cd $PKG/usr/bin ; rm -rf open-simh-id16 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/id16 open-simh-id16 )
( cd $PKG/usr/bin ; rm -rf open-simh-id32 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/id32 open-simh-id32 )
( cd $PKG/usr/bin ; rm -rf open-simh-imlac )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/imlac open-simh-imlac )
( cd $PKG/usr/bin ; rm -rf open-simh-infoserver100 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/infoserver100 open-simh-infoserver100 )
( cd $PKG/usr/bin ; rm -rf open-simh-infoserver1000 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/infoserver1000 open-simh-infoserver1000 )
( cd $PKG/usr/bin ; rm -rf open-simh-infoserver150vxt )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/infoserver150vxt open-simh-infoserver150vxt )
( cd $PKG/usr/bin ; rm -rf open-simh-intel-mds )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/intel-mds open-simh-intel-mds )
( cd $PKG/usr/bin ; rm -rf open-simh-lgp )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/lgp open-simh-lgp )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax1 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax1 open-simh-microvax1 )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax2 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax2 open-simh-microvax2 )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax2000 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax2000 open-simh-microvax2000 )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax3100 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax3100 open-simh-microvax3100 )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax3100e )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax3100e open-simh-microvax3100e )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax3100m80 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax3100m80 open-simh-microvax3100m80 )
( cd $PKG/usr/bin ; rm -rf open-simh-microvax3900 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/microvax3900 open-simh-microvax3900 )
( cd $PKG/usr/bin ; rm -rf open-simh-nova )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/nova open-simh-nova )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp1 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp1 open-simh-pdp1 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp10 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp10 open-simh-pdp10 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp10-ka )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp10-ka open-simh-pdp10-ka )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp10-ki )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp10-ki open-simh-pdp10-ki )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp10-kl )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp10-kl open-simh-pdp10-kl )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp10-ks )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp10-ks open-simh-pdp10-ks )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp11 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp11 open-simh-pdp11 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp15 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp15 open-simh-pdp15 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp4 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp4 open-simh-pdp4 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp6 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp6 open-simh-pdp6 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp7 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp7 open-simh-pdp7 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp8 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp8 open-simh-pdp8 )
( cd $PKG/usr/bin ; rm -rf open-simh-pdp9 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/pdp9 open-simh-pdp9 )
( cd $PKG/usr/bin ; rm -rf open-simh-rtvax1000 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/rtvax1000 open-simh-rtvax1000 )
( cd $PKG/usr/bin ; rm -rf open-simh-s3 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/s3 open-simh-s3 )
( cd $PKG/usr/bin ; rm -rf open-simh-scelbi )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/scelbi open-simh-scelbi )
( cd $PKG/usr/bin ; rm -rf open-simh-sds )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/sds open-simh-sds )
( cd $PKG/usr/bin ; rm -rf open-simh-sel32 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/sel32 open-simh-sel32 )
( cd $PKG/usr/bin ; rm -rf open-simh-sigma )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/sigma open-simh-sigma )
( cd $PKG/usr/bin ; rm -rf open-simh-ssem )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/ssem open-simh-ssem )
( cd $PKG/usr/bin ; rm -rf open-simh-swtp6800mp-a )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/swtp6800mp-a open-simh-swtp6800mp-a )
( cd $PKG/usr/bin ; rm -rf open-simh-swtp6800mp-a2 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/swtp6800mp-a2 open-simh-swtp6800mp-a2 )
( cd $PKG/usr/bin ; rm -rf open-simh-tt2500 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/tt2500 open-simh-tt2500 )
( cd $PKG/usr/bin ; rm -rf open-simh-tx-0 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/tx-0 open-simh-tx-0 )
( cd $PKG/usr/bin ; rm -rf open-simh-uc15 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/uc15 open-simh-uc15 )
( cd $PKG/usr/bin ; rm -rf open-simh-vax )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax open-simh-vax )
( cd $PKG/usr/bin ; rm -rf open-simh-vax730 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax730 open-simh-vax730 )
( cd $PKG/usr/bin ; rm -rf open-simh-vax750 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax750 open-simh-vax750 )
( cd $PKG/usr/bin ; rm -rf open-simh-vax780 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax780 open-simh-vax780 )
( cd $PKG/usr/bin ; rm -rf open-simh-vax8200 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax8200 open-simh-vax8200 )
( cd $PKG/usr/bin ; rm -rf open-simh-vax8600 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vax8600 open-simh-vax8600 )
( cd $PKG/usr/bin ; rm -rf open-simh-vaxstation3100m30 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vaxstation3100m30 open-simh-vaxstation3100m30 )
( cd $PKG/usr/bin ; rm -rf open-simh-vaxstation3100m38 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vaxstation3100m38 open-simh-vaxstation3100m38 )
( cd $PKG/usr/bin ; rm -rf open-simh-vaxstation3100m76 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vaxstation3100m76 open-simh-vaxstation3100m76 )
( cd $PKG/usr/bin ; rm -rf open-simh-vaxstation4000m60 )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vaxstation4000m60 open-simh-vaxstation4000m60 )
( cd $PKG/usr/bin ; rm -rf open-simh-vaxstation4000vlc )
( cd $PKG/usr/bin ; ln -sf ../../opt/open-simh/bin/vaxstation4000vlc open-simh-vaxstation4000vlc )


rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv  doc/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

( cd $PKG/opt/$PRGNAM ; rm -rf doc )
( cd $PKG/opt/$PRGNAM ; ln -sf ../../usr/doc/$PRGNAM-$VERSION doc )

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
